/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 2/11/2004
 *
 */

package au.com.lastweekend.jim.swing.tree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Rectangle;

import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.tree.TreeCellRenderer;

import au.com.lastweekend.jim.swing.TriStateCheckBox;
import au.com.lastweekend.jim.util.TriState;

/**
 * Uses techniques from DefaultTreeCellRenderer but implements a tri state check box over either TriStateSelectableTreeNode or
 * SelectableTreeNode
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: KeywordTreeCellRenderer.java,v 1.3 2006/02/19 06:04:54 ggardner Exp $
 */
@SuppressWarnings("serial")
public class TriStateTreeCellRenderer extends TriStateCheckBox implements TreeCellRenderer {

    private Color selectionForeground;
    private Color selectionBackground;
    private Color textForeground;
    private Color textBackground;

    public TriStateTreeCellRenderer() {

        this.setHorizontalTextPosition(SwingConstants.RIGHT);
        this.setFont(null);
        selectionForeground = UIManager.getColor("Tree.selectionForeground");
        selectionBackground = UIManager.getColor("Tree.selectionBackground");
        textForeground = UIManager.getColor("Tree.textForeground");
        textBackground = UIManager.getColor("Tree.textBackground");

    }

    /*
     * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean,
     *      boolean, int, boolean)
     */
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf,
            int row, boolean hasFocus) {

        String textValue = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);

        this.setState(getNodeState(value));
        this.setEnabled(getNodeEnabled(value));
        this.setText(textValue);
        
        
        
        if (selected) {
            this.setForeground(selectionForeground);
            this.setBackground(selectionBackground);
        } else {
            this.setForeground(textForeground);
            this.setBackground(textBackground);
        }
        return this;
    }

    protected boolean getNodeEnabled(Object value) {

        return true;
    }

    protected TriState getNodeState(Object value) {

        if (value instanceof TriStateTreeNode) {
            TriStateTreeNode node = (TriStateTreeNode) value;

            return node.getSelectedState();

        } else if (value instanceof SelectableTreeNode) {
            SelectableTreeNode node = (SelectableTreeNode) value;
            return node.getSelected() ? TriState.SELECTED : TriState.NOT_SELECTED;

        }

        return TriState.UNKNOWN;
    }

    // Overridden as recommended by DefaultTreeCellRenderer
    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void validate() {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     * 
     * @since 1.5
     */
    public void invalidate() {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void revalidate() {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void repaint(long tm, int x, int y, int width, int height) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void repaint(Rectangle r) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     * 
     * @since 1.5
     */
    public void repaint() {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {

        // Strings get interned...
        if (propertyName == "text"
                || ((propertyName == "font" || propertyName == "foreground") && oldValue != newValue && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {

            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, char oldValue, char newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, short oldValue, short newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, int oldValue, int newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, long oldValue, long newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, float oldValue, float newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, double oldValue, double newValue) {

    }

    /**
     * Overridden for performance reasons. See the <a href="#override">Implementation Note</a> for more information.
     */
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {

    }

}